package com.example.pkmedical.Activity.weight.options;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.pkmedical.APP.MyApp;
import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.SPUtils;
import com.example.pkmedical.Utils.TimeUtils;
import com.example.pkmedical.Utils.ToastUtils;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OptionChangeWeightActivity extends BaseActivity {


    @BindView(R.id.hand_change_yellow_text)
    TextView handChangeYellowText;
    @BindView(R.id.change_left)
    ImageView changeLeft;
    @BindView(R.id.change_left_text)
    TextView changeLeftText;
    @BindView(R.id.change_seekbar)
    SeekBar changeSeekbar;
    @BindView(R.id.change_right_text)
    TextView changeRightText;
    @BindView(R.id.change_right)
    ImageView changeRight;
    @BindView(R.id.change_hand_button_ok)
    Button changeHandButtonOk;
    @BindView(R.id.change_hand_button_cancle)
    Button changeHandButtonCancle;
    @BindView(R.id.change_seek_layout)
    LinearLayout changeSeekLayout;
    @BindView(R.id.change_title_text)
    TextView changeTitleText;
    @BindView(R.id.change_onelayout)
    LinearLayout changeOnelayout;
    @BindView(R.id.change_twolayout)
    LinearLayout changeTwolayout;
    private double nowWeight;

    //true:调整 false：已调整
//    private boolean type = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changeweight);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {

        changeSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

                if (i > 1) {
                    nowWeight = i;
                    switch (CommandUtil.COMPANY_TYPE) {
                        case 0:
                            handChangeYellowText.setText(nowWeight + getString(R.string.kg));
                            break;
                        case 1:
                            handChangeYellowText.setText(nowWeight + getString(R.string.bang));
                            break;
                    }

                } else {
                    seekBar.setProgress(1);
                    nowWeight = 1;
                    switch (CommandUtil.COMPANY_TYPE) {
                        case 0:
                            handChangeYellowText.setText(nowWeight +  getString(R.string.kg));
                            break;
                        case 1:
                            handChangeYellowText.setText(nowWeight + getString(R.string.bang));
                            break;
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void initView() {
        if ((SPUtils.getInstance().getString("user_now_weight").equals("不适用"))) {
            nowWeight = 100;
        } else {
            nowWeight = Double.parseDouble(((SPUtils.getInstance().getString("user_now_weight"))));
        }


        switch (CommandUtil.COMPANY_TYPE) {
            case 0:
                handChangeYellowText.setText(nowWeight + getString(R.string.kg));
                changeRightText.setText("226");
                changeSeekbar.setMax(226);
                changeSeekbar.setProgress((int) nowWeight);
                break;
            case 1:
                handChangeYellowText.setText(nowWeight * 2 + getString(R.string.bang));
                changeRightText.setText("497");
                changeSeekbar.setMax(497);
                changeSeekbar.setProgress((int) nowWeight * 2);
                break;
        }
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick({R.id.change_left, R.id.change_right, R.id.change_hand_button_ok, R.id.change_hand_button_cancle, R.id.hand_change_yellow_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.change_left:
                if (nowWeight > 1) {
                    nowWeight = nowWeight - 1;
                    changeSeekbar.setProgress((int) nowWeight);
                }
                break;
            case R.id.change_right:
                if (nowWeight < 226) {
                    nowWeight = nowWeight + 1;
                    changeSeekbar.setProgress((int) nowWeight);
                }
                break;
            case R.id.change_hand_button_ok:
                twoLayout();
                Toast.makeText(this, "当前调整重量为：" + handChangeYellowText.getText().toString(), Toast.LENGTH_SHORT).show();
                switch (CommandUtil.COMPANY_TYPE) {
                    case 0:
                        SPUtils.getInstance().put("user_now_weight", String.valueOf(nowWeight));
                        break;
                    case 1:
                        SPUtils.getInstance().put("user_now_weight", String.valueOf(nowWeight/2));
                        break;
                }

                SPUtils.getInstance().put("Last_time", TimeUtils.getNowDataAll());
                break;
            case R.id.change_hand_button_cancle:
                finish();
                break;
            case R.id.hand_change_yellow_text:
                oneLayout();
                break;
        }
    }

    private String ellipsis(double numberDecimal) {
        DecimalFormat format = new DecimalFormat("#.##");
        String numberStr = format.format(numberDecimal);
        return numberStr;
    }

    private void oneLayout() {
        changeHandButtonOk.setVisibility(View.VISIBLE);
        changeSeekLayout.setVisibility(View.VISIBLE);
        changeOnelayout.setVisibility(View.VISIBLE);
        changeTwolayout.setVisibility(View.GONE);
    }

    private void twoLayout() {
        changeHandButtonOk.setVisibility(View.GONE);
        changeSeekLayout.setVisibility(View.GONE);
        changeOnelayout.setVisibility(View.GONE);
        changeTwolayout.setVisibility(View.VISIBLE);
    }
}
